//
//  CommonViewController.m
//  Fragrance
//
//  Created by dllo on 15/5/22.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "CommonViewController.h"

@interface CommonViewController ()

@end

@implementation CommonViewController

- (void)dealloc
{
    [_array release];
    [_arrayData release];
    [super dealloc];
}

- (NSMutableArray *)arrayData
{
    if (!_arrayData) {
        _arrayData = [[NSMutableArray alloc] init];
    }
    return _arrayData;
}

- (NSMutableArray *)array
{
    if (!_array) {
        _array = [[NSMutableArray alloc] init];
    }
    return _array;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [TAOverlay showOverlayWithLabel:nil Options:TAOverlayOptionOverlaySizeRoundedRect];
    
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 500)];
    UIImage *image = [UIImage imageNamed:@"beijingtu6"];
    imageV.image = image;
    
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
    label.backgroundColor = [UIColor colorWithRed:250 / 255.0 green:239 / 255.0 blue:182 / 255.0 alpha:1.0];
    [imageV addSubview:label];
    
    [self.view addSubview:imageV];
    
    [imageV release];
    
    
    self.navigationItem.title = @"美食秀";
    [self downLoadPageData];
    
    currentPage = 1;
}




- (void )downLoadPageData
{
   
     NSString *str = [NSString stringWithFormat:@"http://api.mishi.cn/index/goods/?p-pv=1.0&p-rtType=json_orig_result&p-apiv=2.1&categoryId=%ld&city=%%E5%%A4%%A7%%E8%%BF%%9E%%E5%%B8%%82&currentPage=1&lat=38.88356329563114&lng=121.5448609313511&p-apiv=2.1&pageSize=10&sortType=1", self.categoryID];
   str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:50] ;
    request.HTTPMethod = @"GET";
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        [TAOverlay hideOverlay];
        
        NSError *error = nil;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
        NSDictionary *dictionary = [dic objectForKey:@"data"];
        NSArray *array = [dictionary objectForKey:@"resultList"];
        
        for (NSDictionary *otherDic in array) {
            CftModelData *model = [[CftModelData alloc] initWithDictionary:otherDic[@"goodsItem"]];
            
            if (model.name != nil ) {
                
                [self.arrayData addObject:model];
            }
        }
        
        [self myTableView];
        [mTableView reloadData];
    
    }];
     
    
    
}



- (void)myTableView
{
    mTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 50) style:UITableViewStylePlain];
    [self.view addSubview:mTableView];
    mTableView.delegate = self;
    mTableView.dataSource = self;
    [mTableView addFooterWithTarget:self action:@selector(shangLa)];
    [mTableView release];
   
}

- (void)shangLa
{
    
    currentPage++;
    NSString *str = [NSString stringWithFormat:@"http://api.mishi.cn/index/goods/?p-pv=1.0&p-rtType=json_orig_result&p-apiv=2.1&categoryId=%ld&city=%%E5%%A4%%A7%%E8%%BF%%9E%%E5%%B8%%82&currentPage=%ld&lat=38.88356329563114&lng=121.5448609313511&p-apiv=2.1&pageSize=10&sortType=1", self.categoryID, currentPage];
    
    
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:50];
    request.HTTPMethod = @"GET";
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSError *error = nil;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
        NSDictionary *dictionary = [dic objectForKey:@"data"];
        NSArray *array = [dictionary objectForKey:@"resultList"];
        for (NSDictionary *otherDic in array) {
            CftModelData *model = [[CftModelData alloc] initWithDictionary:otherDic[@"goodsItem"]];
            
            if (model.name != nil ) {
                
                [self.arrayData addObject:model];
            }
            
        }
        [mTableView reloadData];
        [mTableView footerEndRefreshing];

    }];
    

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *indentifile = @"cell";
//   CommonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifile];
   
//    if (cell == nil) {
       CommonTableViewCell *cell = [[CommonTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:indentifile] ;
//    }
    cell.layer.borderColor = [UIColor greenColor].CGColor;
    cell.layer.borderWidth = 1;
    
    CftModelData *model = [self.arrayData objectAtIndex:indexPath.row];
    cell.model = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    UIImage *image = [UIImage imageNamed:@"beijing4"];
    UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
    cell.backgroundView = imageview;
    [imageview release];
   
    
    return cell;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrayData.count;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 300;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CftModelData *model = [self.arrayData objectAtIndex:indexPath.row];
    DetailsViewController *details = [[DetailsViewController alloc] init];
    details.model = model;
    [self.navigationController pushViewController:details animated:YES];
    [details release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
